import java.util.HashMap;
import java.util.Map;

class P11239_39643 {

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuilder buffer = new StringBuilder();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString());
	}

	public static void main(String[] args) {

		String line = readLn();
		
		while (true) {
			if (line.charAt(0) == '0') return;
			
			HashMap<String, Integer> projects = new HashMap<String, Integer>();
			HashMap<String, HashMap<String, String>> students = new HashMap<String, HashMap<String,String>>();
			
			while (line.charAt(0) != '1') {
//System.out.println("-> " + line);
				String project = null;
				if (line.charAt(0) <= 'Z' && line.charAt(0) >= 'A') {
					projects.put(line, 0);
					project = line;
				}
				
				String studentOrAnotherProject = readLn();
				line = studentOrAnotherProject;
				// if student
//System.out.println("--> " + studentOrAnotherProject);				
				while (studentOrAnotherProject.charAt(0) <= 'z' && studentOrAnotherProject.charAt(0) >= 'a') {
					
					HashMap<String, String> projectsSignedByStudent = students.get(studentOrAnotherProject);
//System.out.println("ah+ " + projectsSignedByStudent);					
					if (projectsSignedByStudent == null) {
						projectsSignedByStudent = new HashMap<String, String>();
						students.put(studentOrAnotherProject, projectsSignedByStudent);
					}
					
					projectsSignedByStudent.put(project, project);
					
					studentOrAnotherProject = readLn();
					line =  studentOrAnotherProject;
//System.out.println("-> -> " + line);					
				}
				
			}
			
			
			
			for (Map.Entry<String, HashMap<String, String>> entry : students.entrySet()) {
				
				if (entry.getValue().size() == 1) {
					String key = entry.getValue().entrySet().iterator().next().getKey();
					
					Integer number = projects.get(key) + 1;
					projects.put(key, number);
					
				}
				
			}
			
			String[] projectsArray = new String[projects.size()];
			int[] projectsNumbersArray = new int[projects.size()];
			
			int k = 0;
			
			for (Map.Entry<String, Integer> entry : projects.entrySet()) {
				projectsArray[k] = entry.getKey();
				projectsNumbersArray[k] = entry.getValue();
				k++;
			}
			
			
			ordena(projectsNumbersArray, projectsArray);
			
			for (k = projectsNumbersArray.length - 1; k >= 0; k--) {
				System.out.println(projectsArray[k] + " " + projectsNumbersArray[k]);
			}
			
//			System.out.println("projects: " + projects);
//			System.out.println("students: " + students);
			
			line = readLn();
			
		}
		
	}
	
	
	private static void quickSort(int[] ocorrencias, String[] projects, int i, int j) {
		if (i < j) {
			int pivot = particiona(ocorrencias, projects, i, j);
			quickSort(ocorrencias, projects, i, pivot-1);
			quickSort(ocorrencias, projects, pivot+1, j);
		}
	}

	private static int particiona(int[] ocorrencias, String[] projects, int i, int j) {
		int pivot = j;
		for (int k = pivot-1; k >= 0; k--) {
			if (ocorrencias[pivot] < ocorrencias[k] || ( ocorrencias[pivot] == ocorrencias[k] && projects[pivot].compareTo(projects[k]) > 0)) {
				troca(k, pivot-1, ocorrencias, projects);
				troca(pivot-1, pivot, ocorrencias, projects);
				pivot = pivot-1;
			}
		}
		return pivot;
	}
	private static void troca(int k, int i, int[] ocorrencias, String[] projects) {
		int auxInt = ocorrencias[k];
		ocorrencias[k] = ocorrencias[i];
		ocorrencias[i] = auxInt;
		String auxAscii = projects[k];
		projects[k] = projects[i];
		projects[i] = auxAscii;
	}


	private static void ordena(int[] frequencias, String[] projects) {
		quickSort(frequencias, projects, 0, frequencias.length - 1);
		
	}

}
